import { createRouter, createWebHistory } from 'vue-router'
import Cookies from 'cookies-js'
import routes from './route'

const router = createRouter({
    routes,
    history: createWebHistory(process.env.BASE_URL)
})

router.beforeEach((to, from, next) => {
    const isLogin = Cookies.get('ssoSessionId')

	if (to.matched.some(record => record.meta.requiresAuth)) {
		// eslint-disable-next-line no-constant-condition
		if (!isLogin) {
			next({
				path: '/login'
			})
		} else {
			next()
		}
	} else {
		next()
	}
})

router.afterEach(route => {
	document.title = route.meta.title
	window.scrollTo(0, 0)
    document.getElementById('content') && document.getElementById('content').scrollTo(0, 0)
	// NProgress.done()
})

export default router